package com.bacsumu.boogservice.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.bacsumu.boogservice.common.util.GAEUtil;
import com.bacsumu.boogservice.entity.HelloWorld;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@Controller
@SessionAttributes
public class WelcomeController {
	@RequestMapping("/index.htm")
	public ModelAndView index(){
		String message = "Hello World Saved";
		return new ModelAndView("welcome/index", "message",message);
	}
	
	@RequestMapping("/tile.htm")
	public ModelAndView showTile() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("index.layout");         /* Tiles 설정파일의 definition name을 아규먼트로 사용하여 Tiles를 실행한다 */
		mav.addObject("index.layout", "감사해요, Spring2.5");
		//return mav;
		return new ModelAndView(".contact", "message","감사해요, Spring2.5");
	}
	
	@RequestMapping("/contacts.htm")
	public ModelAndView showContacts() {
		
		return new ModelAndView(".contact", "command", "");
	}
	
	@RequestMapping("/login.htm")
	public ModelAndView googleLogin() {
		// 사용자 정보를 구한다.
		User l_user = GAEUtil.getCurrentUser();
		
		// 현재 도메인 정보를 구하기 위해서 값을 가져온다.
		HttpServletRequest l_hsr = GAEUtil.getRequest();
		
		boolean l_isLogin = false;
		String l_callUrl;
		// 로그인 상태
		if(l_user!=null){
			l_isLogin = true;
			l_callUrl = GAEUtil.getLogoutURL(l_hsr.getRequestURI());
		}else{
			l_isLogin = false;
			l_callUrl = GAEUtil.getLoginURL(l_hsr.getRequestURI());
		}
		return new ModelAndView("welcome/googlelogin", "isLogin", l_isLogin)
					.addObject("callUrl", l_callUrl).addObject("user", l_user);
	}
}
